package com.example.demo.filters;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;

import java.io.IOException;

/**
 * 过滤器
 */

@WebFilter(urlPatterns = "/*")  // webFilter注册 需配置扫描
public class MyFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // init
        System.out.println("Filter init");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        System.out.println("Filter doFilter");
        chain.doFilter(request, response);
        System.out.println("Filter doFilter leave");

    }

    @Override
    public void destroy() {
        System.out.println("Filter destroy");
    }
}



//import org.springframework.boot.web.servlet.FilterRegistrationBean;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//
//@Configuration
//public class FilterConfig {
//
//    @Bean
//    public FilterRegistrationBean<MyFilter> loggingFilter(){
//        FilterRegistrationBean<MyFilter> registrationBean = new FilterRegistrationBean<>();
//
//        // 注册自定义过滤器实例
//        registrationBean.setFilter(new MyFilter());
//
//        // 指定过滤器应用的 URL 模式
//        registrationBean.addUrlPatterns("/*");
//
//        // 设置过滤器优先级，值越低优先级越高
//        registrationBean.setOrder(1);
//
//        return registrationBean;
//    }
//}
